import * as vscode from 'vscode';
import { DataItem } from '../TreeViewProviderN';
import * as fs from 'fs';

export function readPathsSigns( item :  DataItem ): Promise<any>{
    const paths : any = item.options?.paths || [];
    const workspace = vscode.workspace;

    return new Promise( ( res )=>{
        var num:number = 0 , re : Array<any> = [];
        function check(){
            if( num === paths.length ){
                res( re );
            }
        }
        for(let i in paths){
            let fd : any = workspace.rootPath + "/" + paths[i];
            if( fs.existsSync(fd) ){
                if( fs.statSync(fd).isFile() ){
                    let fileurl = vscode.Uri.file( fd );
                    let reglist:Array<any> = [];

                    
                    workspace.openTextDocument(fileurl)
                    .then(function(file) {
                        var regex = new RegExp("(?:\@LINKS)\\s*\\W{0,1}(\\s+.*|(?:\\w|\\d).*)$" , "gi");
                        // findTodosinSpecifiedFile(file, todos, todosList, scanRegex);
                        let fileInUri = file.uri.toString();
                        let pathWithoutFile = fileInUri.substring(7, fileInUri.length);

                        for (var line = 0; line < file.lineCount; line++) {
                            var lineText = file.lineAt(line).text;
                            var match = regex.exec(lineText); //lineText.match(regex);

                            if (match !== null) {
                                re.push({
                                    line : line+1,
                                    text : match[1],
                                    file : fileurl
                                });
                            }
                        }
                    });
                    num+=1;
                    check();
                }else if( fs.statSync(fd).isDirectory() ){
                    let reg = new vscode.RelativePattern( workspace.rootPath + "/" + paths[i] , `**/**/*.{ts,js,jsx,vue,json}`);
                    workspace.findFiles( reg , "", 10000)
                    .then(( filelist )=>{
                        // re = re.concat( [], filelist );                        
                    
                        for(let j in filelist){
                            (()=>{
                                workspace.openTextDocument(filelist[j])
                                .then(function(file) {
                                    var regex = new RegExp("(?:\@LINKS)\\s*\\W{0,1}(\\s+.*|(?:\\w|\\d).*)$" , "gi");
                                    // findTodosinSpecifiedFile(file, todos, todosList, scanRegex);        
                                    for (var line = 0; line < file.lineCount; line++) {
                                        var lineText = file.lineAt(line).text;
                                        var match = regex.exec(lineText); //lineText.match(regex);
        
                                        if (match !== null) {
                                            re.push({
                                                line : line+1,
                                                text : match[1],
                                                file : filelist[j]
                                            });
                                        }
                                    }

                                    if(Number(j)+1 === filelist.length ){
                                        num+= 1;
                                        check();
                                    }
                                });
                            })();
                        }
                    });
                }else{
                    num+=1;
                    check();
                }
            }else{
                num+=1;
                check();
            }
        }
    });
}

// export function findTodosinSpecifiedFile( file, todos, todosList, scanRegex) {
//     var fileInUri = file.uri.toString();
//     var pathWithoutFile = fileInUri.substring(7, fileInUri.length);
    
//     var regex = new RegExp(scanRegex, "i");
    
//     for (var line = 0; line < file.lineCount; line++) {
//         var lineText = file.lineAt(line).text;
//         var match = lineText.match(regex);
//         if (match !== null) {
//             if (!todos.hasOwnProperty(pathWithoutFile)) {
//                 todos[pathWithoutFile] = [];
//             }
//             var todoMessage = getTodoMessage(lineText, match);
//             var todoLocation = getTodoLocation(pathWithoutFile, todoMessage, line, match);
//             var todo = new Todo(todoLocation, pathWithoutFile, line, todoMessage, 10);
//             todosList.push(todo);
//             todos[pathWithoutFile].push(todo);
//         }
//     }
// };